﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DataModel;
using System.Collections.Generic;
using A_ReferenceCodes.Services.Admin.RefCodes;
using System.Linq;
namespace A_ReferenceCodes
{
    public abstract class DM_CodeTypeBase:DataModelBase
    {
        protected CodeType CT;

        public string Name
        {
            get { return CT.Name; }
            set
            {
                if (CT.Name != value)
                {
                    CT.Name = value;
                    OnPropertyChanged("Name");
                }
            }
        }

        public string Description
        {
            get { return CT.Description; }
            set
            {
                if (CT.Description != value)
                {
                    CT.Description = value;
                    OnPropertyChanged("Description");
                }
            }
        }

        public string MetaDataTitle
        {
            get { return CT.MetaDataTitle; }
            set
            {
                if (CT.MetaDataTitle != value)
                {
                    CT.MetaDataTitle = value;
                    OnPropertyChanged("MetaDataTitle");
                }
            }
        }

        public int? MetaDataType
        {
            get { return CT.MetaDataType; }
            set
            {
                if (CT.MetaDataType != value)
                {
                    CT.MetaDataType = value;
                    OnPropertyChanged("MetaDataType");
                }
            }
        }

        protected override void ComitNewObject()
        {
            throw new NotImplementedException();
        }

        public static DM_CodeTypeBase GetCodeType(CodeType CT)
        {
            switch (CT.MetaDataType)
            {
                case 1:
                    return new DM_CodeType<bool>(CT);
                default:
                    //String defaults to empty so it's perfect for this task.
                    //If you use int then a 0 will appear in the top left of 
                    //the grid view after switching away from one which does 
                    //display the metadata colun
                    return new DM_CodeType<string>(CT);
            }
        }
        //public abstract List<DM_RefCodeBase> RefCodes
        //{
        //    get;
        //}

        public Visibility IsMetaDataVisible
        {
            get
            {
                if (CT.MetaDataType == null)
                    return Visibility.Collapsed;
                else
                    return Visibility.Visible;
            }
        }

        public void NewRefCode(string Description)
        {
            DM_RefCodeBase RC = new DM_RefCodeBase(CT.ID);
            RC.NewObject();
            RC.Description = Description;
            if (CT.RefCodes!= null & CT.RefCodes.Count >0)
            {
                RC.Weight = CT.RefCodes.Select((R) => R.Weight).Max() + 5;
            }
            else
            {
                RC.Weight = 0;
            }
            if (Description.Length > 5)
            {
                RC.ShortName = Description.Substring(0, 5);
            }
            else
            {
                RC.ShortName = Description;
            }
            DataModel.ChangeTracking.AcceptChanges();
            CT.RefCodes.Add(RC.RC);
            OnPropertyChanged("RefCodes");
        }
    }

    public class DM_CodeType<T> : DM_CodeTypeBase
    {





        public DM_CodeType(CodeType CT)
        {
            base.CT = CT;
        }

        //private List<DM_RefCode<T>> _RefCodes;
        //public override List<DM_RefCodeBase> RefCodes
        //{
        //    get 
        //    { 
        //        return CT.RefCodes.Select((RC)=>(DM_RefCodeBase)new DM_RefCode<T>(RC)).ToList(); 
        //    }
        //    //set
        //    //{
        //    //    if (_RefCodes != value)
        //    //    {
        //    //        _RefCodes = value;
        //    //        OnPropertyChanged("RefCodes");
        //    //    }
        //    //}
        //}
        public List<DM_RefCode<T>> RefCodes
        {
            get
            {
                return CT.RefCodes.Select((RC) => new DM_RefCode<T>(RC)).ToList();
            }
            //set
            //{
            //    if (_RefCodes != value)
            //    {
            //        _RefCodes = value;
            //        OnPropertyChanged("RefCodes");
            //    }
            //}
        }

    }
}
